package com.bianys.tiwen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bianys.tiwen.entity.Student;
import com.bianys.tiwen.mapper.StudentMapper;
import com.bianys.tiwen.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bianys.tiwen.utils.EmailUtil;
import com.bianys.tiwen.utils.TiwenUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bianys
 * @since 2022-10-05
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Scheduled(cron = "0 50 11 * * ?")
    public void tianTiWen() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 0);
        List<Student> students = baseMapper.selectList(queryWrapper);
        List<Student> failStudents = new ArrayList<>();
        //1.开启邮件服务器
        try {
            EmailUtil.start("2060695019@qq.com", "msrzebocwjolcgcc");
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        //填体温，发邮件
        for (Student student: students) {
            //填体温
            boolean success = TiwenUtil.tianTiWen(student.getAccount(), student.getPasswod());
            //给个人发送邮件
            if (success){
                //体温填写成功
                try {
                    EmailUtil.sendEmail(EmailUtil.txtEmailMessage(student.getEmail(), "体温已填","懒逼"+student.getAccount()+"！我帮你填体温了！"));
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }else {
                //体温填写失败
                failStudents.add(student);
                try {
                    EmailUtil.sendEmail(EmailUtil.txtEmailMessage(student.getEmail(), "体温填写失败","您的体温填写失败，可能是由于本日体温系统需要填写其他信息，请自行登录体温系统查看。"));
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }
        //给管理员发送邮件
        if(failStudents.size()==0){
            //都填成功
            try {
                EmailUtil.sendEmail(EmailUtil.txtEmailMessage("2060695019@qq.com", "全体成员体温填写成功","需要填写体温的人员共"+students.size()+"人，体温已全部填写。"));
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }else {
            //部分失败
            String content = "  需要填写体温的人员共"+students.size()+"人，其中"+(students.size()-failStudents.size())+"人填写成功，"+failStudents.size()+"人填写失败，请提醒填写失败的人员填体温。<br>";
            StringBuilder failStudent = new StringBuilder(content);
            failStudent.append("其中体温填写失败的成员有：<br>");
            for (Student student: failStudents) {
                failStudent.append("account:" + student.getAccount() + " email:"+student.getEmail()+"<br>");
            }
            try {
                EmailUtil.sendEmail(EmailUtil.txtEmailMessage("2060695019@qq.com", "部分成员体温填写失败",failStudent.toString()));
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        //关闭邮件服务器
        EmailUtil.close();
    }
}
